import * as chai from 'chai';
import * as sinon from 'sinon';
import proxyquire from 'proxyquire'
import path from 'path'

const expect = chai.expect;

describe('getNodeVersionFromFile', () => {
    let fsStub: any;
    let getNodeVersionFromFileProxy: (versionFilePath: string) => string | null;

    beforeEach(() => {
        fsStub = {
            existsSync: sinon.stub(),
            readFileSync: sinon.stub()
        };

        // 使用 proxyquire 替换 fs 模块
        const utilModule = proxyquire('../src/util', {
            fs: fsStub
        });

        getNodeVersionFromFileProxy = utilModule.getNodeVersionFromFile;
    });

    afterEach(() => {
        sinon.restore();
    });

    it('should return null if the file does not exist', () => {
        const filePath = '/path/to/nonexistent/file';
        fsStub.existsSync.returns(false);

        const result = getNodeVersionFromFileProxy(filePath);
        expect(result).to.be.null;
    });

    it('should return the trimmed content of the file if it exists', () => {
        const filePath = '/path/to/existent/file';
        const fileContent = ' 14.17.0 \n';
        const expectedOutput = '14.17.0';

        fsStub.existsSync.returns(true);
        fsStub.readFileSync.returns(fileContent);

        const result = getNodeVersionFromFileProxy(filePath);
        expect(result).to.equal(expectedOutput);
    });
});

describe('resolveVersion', () => {
    let fsStub: any;
    let resolveVersionProxy: (workSpace: string, versionType: string | undefined, inputVersion: string | undefined) => string;

    beforeEach(() => {
        fsStub = {
            existsSync: sinon.stub(),
            readFileSync: sinon.stub()
        };

        // 使用 proxyquire 替换 fs 模块和 getNodeVersionFromFile 函数
        const utilModule = proxyquire('../src/util', {
            fs: fsStub
        });

        resolveVersionProxy = utilModule.resolveVersion;
    });

    afterEach(() => {
        sinon.restore();
    });

    it('should throw an error if versionType is undefined', () => {
        expect(() => resolveVersionProxy('/workspace', undefined, '14.17.0')).to.throw('missing version type');
    });

    it('should throw an error if .nvmrc file does not exist and versionType is NVMRC', () => {
        expect(() => resolveVersionProxy('/workspace', 'NVMRC', undefined)).to.throw('Get node version from .nvmrc but could not determine node version from /workspace/.nvmrc.');
    });

    it('should return the version from .nvmrc if versionType is NVMRC and file exists', () => {
        const filePath = '/path/to/existent/file';
        const fileContent = ' 14.17.0 \n';
        const expectedOutput = '14.17.0';

        fsStub.existsSync.returns(true);
        fsStub.readFileSync.returns(fileContent);

        const result = resolveVersionProxy('/workspace', 'NVMRC', undefined);
        expect(result).to.equal('14.17.0');
    });

    it('should throw an error if inputVersion is undefined and no version found in .nvmrc', () => {
        expect(() => resolveVersionProxy('/workspace', 'OTHER', undefined)).to.throw('missing input version');
    });

    it('should return the inputVersion if provided and no version found in .nvmrc', () => {
        const result = resolveVersionProxy('/workspace', 'OTHER', '14.17.0');
        expect(result).to.equal('14.17.0');
    });
});